package com.ruoyi.common.mytools;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.apache.commons.lang3.RegExUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Tools {
    static Random randObj = new Random();
    // 生成6位随机验证码
    public static String generateCode6() {
        return Integer.toString(100000 + randObj.nextInt(900000));
    }
    // 生成5位随机验证码
    public static String generateCode5() {
        return Integer.toString(10000 + randObj.nextInt(90000));
    }
    // 生成4位随机验证码
    public static String generateCode4() {
        return Integer.toString(1000 + randObj.nextInt(9000));
    }
    // 生成2位随机验证码
    public static String generateCode2() {
        return Integer.toString(10 + randObj.nextInt(50));
    }
    //生成uuid
    public static String generateUUID(){
            UUID randomUUID = UUID.randomUUID();
            return randomUUID.toString().replaceAll("-", "");
    }
    //生成随机字符串
    public static String generateRandomStr(int length){
        String alphabetsInUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String alphabetsInLowerCase = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "0123456789";
        // create a super set of all characters
        String allCharacters = alphabetsInLowerCase + alphabetsInUpperCase + numbers;
        // initialize a string to hold result
        StringBuffer randomString = new StringBuffer();
        // loop for 10 times
        for (int i = 0; i < length; i++) {
            // generate a random number between 0 and length of all characters
            int randomIndex = randObj.nextInt(allCharacters.length());
            // retrieve character at index and add it to result
            randomString.append(allCharacters.charAt(randomIndex));
        }
        return randomString.toString();
    }
    //生成#A+26字母随机字符
    public static String generateRandomAand26(int length){
        String alphabetsInUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        // create a super set of all characters
        String allCharacters =alphabetsInUpperCase;
        // initialize a string to hold result
        StringBuffer randomString = new StringBuffer();
        // loop for 10 times
        for (int i = 0; i < length; i++) {
            // generate a random number between 0 and length of all characters
            int randomIndex = randObj.nextInt(allCharacters.length());
            // retrieve character at index and add it to result
            randomString.append("#Z"+allCharacters.charAt(randomIndex));
        }
        return randomString.toString();
    }

    /*正则表达式获取图片链接*/
    public static List<String> getImageTag(String htmlContent){

        List<String> imageSrcs = new ArrayList<>();
        List<String> imgUrlList = new ArrayList<>();
        Pattern reg = Pattern.compile("<img[^>]+\\/>");
        Matcher matcher = reg.matcher(htmlContent);
//        System.out.println("matcher....");
//        System.out.println(matcher);


//        Regex reg = new Regex("<img\\[^>\\]+\\/>", RegexOptions.IgnoreCase);
//        MatchCollection matcher = reg.Matches(content);

        while (matcher.find()) {
            // 获取图片的src属性值
            String imageSrc = matcher.group();
            imageSrcs.add(imageSrc);
        }

//        System.out.println("抓取到图片列表");
//        imageSrcs.forEach(System.out::println);

        //再提取图片链接
        reg = Pattern.compile("src=\".*?\"");
        for (int i = 0; i < imageSrcs.size(); i++) {
            //System.out.println("单个:"+imageSrcs.get(i));
            matcher = reg.matcher(imageSrcs.get(i));
            while (matcher.find()) {
                // 获取图片的src属性值
                String murl = matcher.group();
                imgUrlList.add(murl.replace("src=\"","").replace("/>","").replace("\"","").replace("src=",""));
            }
        }
        //System.out.println("抓取到图片列表");
        //imgUrlList.forEach(System.out::println);

        return imgUrlList;
    }

}
